<?php
require 'BaseController.php';
class Admin_ProductController extends Admin_BaseController{
	private $_identity;
	private $_categoryModel;
	private $_postModel;
	private $_userModel;
	public function init(){
		parent::init();
		$auth = Zend_Auth::getInstance();
		$this->_identity = $auth->getIdentity();
		$this->view->identity = $this->_identity;
		$this->_categoryModel = new Model_Category();
		$this->_postModel = new Model_Post();
		$this->_userModel = new Model_User();
	}
	
	public function listAction(){
		$session = new Zend_Session_Namespace();
		$this->view->message = $session->message;
		$session->__unset('message');
		if($this->getRequest()->isPost()){
			$category_id = $this->getRequest()->getParam("category_id");
			$title = $this->getRequest()->getParam("title");
		}
		$type = $this->getRequest()->getParam("type", "car");
		$this->view->type = $type;
		//Đưa danh sách user vào Zend_Paginator để bắt đầu phân trang
        $paginator = Zend_Paginator::factory($this->_postModel->getAllPosts($type, $title, $category_id));
        //Số user trên một trang
        $paginator->setItemCountPerPage(20);
        //Số trang được hiện ra để click
        $paginator->setPageRange(5);
        //Lấy trang hiện tại
        $currentPage = $this->getRequest()->getParam('page',1);
        $paginator->setCurrentPageNumber($currentPage);
        
        //Truyền dữ liệu ra view
        $this->view->arrProducts=$paginator;
        $this->view->arrCategories = $this->_categoryModel->getAllCategories();
	}
	
	public function createAction(){
		$this->view->identity = $this->_identity;
		$this->view->headScript()->appendFile($this->view->baseUrl().'/public/plugins/ckeditor/ckeditor.js');
		$this->view->headScript()->appendFile($this->view->baseUrl().'/public/plugins/ckfinder/ckfinder.js');
		$this->view->headLink()->appendStylesheet($this->view->baseUrl().'/public/admin/css/aearticle.css');
		$this->view->arrCategories = $this->_categoryModel->getAllCategories();
		$this->view->arrSalesPersons = $this->_userModel->getUserByRole(1);
		$session = new Zend_Session_Namespace();
		$request = $this->getRequest();
		//kiểm tra có dữ liệu post lên không
		if($request->isPost()){
			//lấy dữ liệu trên form
			$title= $request->getParam('title');
			$summary= $request->getParam('summary');
			$content= $request->getParam('content');
			$slug = $request->getParam('slug');
			$image = $request->getParam('image');
			$image = join('@', $image);
			$created_date = date('Y-m-d', time());
			$category_id =$request->getParam("category_id");
			$user_id =$request->getParam("user_id");
			$price = $request->getParam("price", 0);
			$type = $request->getParam("type", "car");
			$featured = $request->getParam("featured");
			$page_title = $this->getRequest()->getParam('page_title');
			$page_description = $this->getRequest()->getParam('page_description');
			$tag = $this->getRequest()->getParam('tag');
			$id = $this->_postModel->insertPost($title, $summary, $content, $created_date, $user_id, $slug, $image, $type ,$page_title, $page_description, $tag, $price, $featured, $category_id);
			if($id){
				$session->message = '<span class="success">Tạo sản phẩm mới thành công</span>';
				$this->_redirector->gotoRoute(array('id' => $id, "type" => $type), 'admin_product_edit');
			}else{
				$this->view->message = '<span class="error">Tạo sản phẩm mới không thành công';
			}
		}
		
	}
	
	public function editAction(){
		$this->view->identity = $this->_identity;
		$this->view->headScript()->appendFile($this->view->baseUrl().'/public/plugins/ckeditor/ckeditor.js');
		$this->view->headScript()->appendFile($this->view->baseUrl().'/public/plugins/ckfinder/ckfinder.js');
		$this->view->headLink()->appendStylesheet($this->view->baseUrl().'/public/admin/css/aearticle.css');
		$session = new Zend_Session_Namespace();
		$this->view->message = $session->message;
		$session->__unset('message');
		$this->view->arrCategories = $this->_categoryModel->getAllCategories();
		$this->view->arrSalesPersons = $this->_userModel->getUserByRole(1);
		$request = $this->getRequest();
		//lấy article
		$id = $request->getParam('id');
		if(isset($id)){
			$product = $this->_postModel->getPostByID($id);
			$this->view->product = $product;
			//kiểm tra có dữ liệu post lên không
			if($this->getRequest()->isPost()){
				//lấy dữ liệu trên form
				$title= $request->getParam('title');
				$summary= $request->getParam('summary');
				$content= $request->getParam('content');
				
				$slug = $request->getParam('slug');
				$image = $request->getParam('image');
				$image = join('@', $image);
				$category_id =$request->getParam("category_id");
				$user_id = $request->getParam("user_id");
				$price = $request->getParam("price", 0);
				$type = $request->getParam("type", "car");
				$featured = $request->getParam("featured");
				echo $featured;
				$page_title = $this->getRequest()->getParam('page_title');
				$page_description = $this->getRequest()->getParam('page_description');
				$tag = $this->getRequest()->getParam('tag');
				$result = $this->_postModel->updatePost($id, $title, $summary, $content, $slug, $image, $page_title, $page_description, $tag, $user_id, $price, $featured, $category_id);
				if($result){
					$session->message = '<span class="success">Sửa sản phẩm thành công</span>';
				}else{
					$this->view->message = '<span class="error">Sửa sản phẩm không thành công</span>';
				}
				$this->_redirector->gotoRoute(array('id'=> $id, "type" => $type), 'admin_product_edit');
				
			}
		}else{
			$this->view->message = '<span class="error">Sản phẩm này không tồn tại</span>';
		}
	}
	public function deleteAction(){
		$session = new Zend_Session_Namespace();
		if($this->getRequest()->isPost()){
			$id = $this->getRequest()->getParam('ids');
		}else{
			$id = $this->getRequest()->getParam('id');
		}
		if($this->_postModel->deletePost($id)){
			$session->message = '<span class="success">Xóa bài viết thành công</span>';
			
		}else{
			$session->message = '<span class="error">Xóa bài viết không thành công</span>';
		}
		$this->_redirector->gotoRoute(array(), 'admin_article_list');
	}
	
}
?>